package com.myPlaneGame.obj;

import com.myPlaneGame.GameCreate;
import com.myPlaneGame.Music.PlayMusic;
import com.myPlaneGame.Utils.GameUtils;

import java.awt.*;
import java.awt.event.*;
import java.io.File;

public class PlaneObj extends GameObj {
    int planeHealth = 3;

    //记录我方飞机碰撞补给的次数（0：一级子弹  1：二级子弹  2：三级子弹）
    public static int times = 0;
    public static int skill = 0;
    //记录技能使用状态
    public static int skillUsing=0;
    public PlaneObj() {
        super();
    }

    public PlaneObj(Image img, int width, int height, int x, int y, double speed, GameCreate frame) {
        super(img, width, height, x, y, speed, frame);
        //添加鼠标的移动事件
        this.frame.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                //让鼠标在中间（减去飞机一半的长宽）
                PlaneObj.super.x = e.getX() - 19;
                PlaneObj.super.y = e.getY() - 20;
            }
        });
        //添加键盘飞机移动事件
       /* this.frame.addKeyListener(new KeyAdapter() {
            //键盘按住不松(目前只能做到直线移动还不能够完成斜线移动)
            @Override
            public void keyPressed(KeyEvent e) {
                int code = e.getKeyCode();
                System.out.println(code);
                //左：37 上：38 右：39 下：40
                if (code == 37 && PlaneObj.super.x > 0 + 10) {
                    PlaneObj.super.x -= 10;//左
                }
                if (code == 39 && PlaneObj.super.x < 600 - 50) {
                    PlaneObj.super.x += 10;//右
                }
                if (code == 38 && PlaneObj.super.y > 0 + 20) {
                    PlaneObj.super.y -= 10;//上
                }
                if (code == 40 && PlaneObj.super.y < 800 - 50) {
                    PlaneObj.super.y += 10;//下
                }
            }
        });*/
    }

    public PlaneObj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        MypaintHP(g);
        //创建boss的对象(用于碰撞检测)
        LittleBoss1 littleBoss1 = new LittleBoss1();
        BossObj bossObj = new BossObj();

        //进行碰撞检测

        //foreach循环
        //for(一个类型进行接受后面的元素:需要遍历的集合的元素)

        //1.我方飞机和敌方小飞机碰撞之后，两者都消失
        for (Enemy1Obj enemy1Obj : GameUtils.enemy1ObjList) {
            if (this.getRec().intersects(enemy1Obj.getRec()) && planeHealth > 0) {
                //碰撞之后出现爆炸动画
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                enemy1Obj.setX(-100);
                enemy1Obj.setY(-100);
                GameUtils.removeList.add(enemy1Obj);
                planeHealth--;
            } else if (this.getRec().intersects(enemy1Obj.getRec())) {
                //碰撞之后出现爆炸动画
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);
                //我方小飞机
                this.x = -200;
                this.y = -200;
                enemy1Obj.setX(-100);
                enemy1Obj.setY(-100);
                //加入到移除游戏窗口集合当中
                GameUtils.removeList.add(this);
                GameUtils.removeList.add(enemy1Obj);
                //游戏失败（飞机血量为0）
                System.out.println("看看我执行了吗1");
                GameCreate.state = 3;
            }
        }
        //2.我方飞机和敌方大飞机碰撞之后，两者都消失
        for (Enemy2Obj enemy2Obj : GameUtils.enemy2ObjList) {
            if (this.getRec().intersects(enemy2Obj.getRec()) && planeHealth > 0) {
                //碰撞之后出现爆炸动画
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);
                //敌方大飞机
                enemy2Obj.setX(-100);
                enemy2Obj.setY(-100);
                GameUtils.removeList.add(enemy2Obj);
                planeHealth--;
            } else if (this.getRec().intersects(enemy2Obj.getRec()) && planeHealth <= 0) {
                //碰撞之后出现爆炸动画
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);
                //我方小飞机
                this.x = -200;
                this.y = -200;
                //加入到移除游戏窗口集合当中
                GameUtils.removeList.add(enemy2Obj);
                GameUtils.removeList.add(this);

                //游戏失败（飞机血量为0）
                System.out.println("看看我执行了吗2");
                GameCreate.state = 3;
            }
        }
        //3.我方飞机和敌方大飞机子弹碰撞之后，两者都消失
        for (Enemy2BulletObj enemy2BulletObj : GameUtils.enemy2BulletList) {
            if (this.getRec().intersects(enemy2BulletObj.getRec()) && planeHealth > 0) {
                //通过改变物体的坐标位置使其显示不出来，做一个逻辑消失
                //敌方大飞机子弹
                enemy2BulletObj.setX(-100);
                enemy2BulletObj.setY(-100);
                GameUtils.removeList.add(enemy2BulletObj);
                System.out.println("看看我执行了吗3");
                planeHealth--;
            } else if (this.getRec().intersects(enemy2BulletObj.getRec()) && planeHealth <= 0) {
                //碰撞之后出现爆炸动画(在飞机加上生命值后要判断生命值为0才爆炸)
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);
                //我方小飞机
                this.x = -200;
                this.y = -200;
                //加入到移除游戏窗口集合当中
                GameUtils.removeList.add(this);
                GameUtils.removeList.add(enemy2BulletObj);
                //游戏失败（飞机血量为0）
                System.out.println("看看我执行了吗3");
                GameCreate.state = 3;
            }
        }
        //4.当敌方boss1和我方飞机碰撞的时候，我方飞机消失，敌方飞机不消失
        if (this.getRec().intersects(littleBoss1.getRec())) {
            //碰撞之后出现爆炸动画(在飞机加上生命值后要判断生命值为0才爆炸)
            ExplodeObj explodeObj = new ExplodeObj(x, y);
            GameUtils.explodeObjList.add(explodeObj);
            GameUtils.removeList.add(explodeObj);

            planeHealth = 0;
            //我方飞机消失
            this.x = -200;
            this.y = -200;
            GameUtils.removeList.add(this);
            //游戏失败（飞机血量为0）
            System.out.println("看看我执行了吗4");
            GameCreate.state = 3;
        }
        //5.当敌方boss2和我方飞机碰撞的时候，我方飞机消失，敌方飞机不消失
        for (LittleBoss2 littleBoss2 : GameUtils.littleBoss2List) {
            if (this.getRec().intersects(littleBoss2.getRec())) {
                //碰撞之后出现爆炸动画(在飞机加上生命值后要判断生命值为0才爆炸)
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                //我方飞机消失
                this.x = -200;
                this.y = -200;
                GameUtils.removeList.add(this);

                //游戏失败（飞机血量为0）
                System.out.println("看看我执行了吗5");
                GameCreate.state = 3;
            }
        }
        //6.我方飞机和敌方boss1子弹碰撞之后，敌方子弹消失，我方飞机不消失
        for (LittleBoss1Bullet littleBoss1Bullet : GameUtils.littleBoss1BulletList) {
            if (this.getRec().intersects(littleBoss1Bullet.getRec()) && planeHealth > 0) {
                //碰撞之后出现爆炸动画
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);
                //敌方boss1子弹消失
                littleBoss1Bullet.setX(-100);
                littleBoss1Bullet.setY(-100);
                GameUtils.removeList.add(littleBoss1Bullet);
                System.out.println("看看我执行了吗6");
                planeHealth--;
            } else if (this.getRec().intersects(littleBoss1Bullet.getRec()) && planeHealth <= 0) {
                //碰撞之后出现爆炸动画(在飞机加上生命值后要判断生命值为0才爆炸)
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);
                //我方飞机消失
                this.x = -200;
                this.y = -200;
                GameUtils.removeList.add(this);
                GameUtils.removeList.add(littleBoss1Bullet);

                //游戏失败（飞机血量为0）
                System.out.println("看看我执行了吗6");
                GameCreate.state = 3;
            }
        }
        //7.我方飞机和敌方boss2子弹碰撞之后，敌方子弹消失，我方飞机不消失
        for (LittleBoss2Bullet littleBoss2Bullet : GameUtils.littleBoss2BulletList) {
            if (this.getRec().intersects(littleBoss2Bullet.getRec()) && planeHealth > 0) {
                //碰撞之后出现爆炸动画
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);
                //敌方boss2子弹消失
                littleBoss2Bullet.setX(-100);
                littleBoss2Bullet.setY(-100);
                GameUtils.removeList.add(littleBoss2Bullet);
                System.out.println("看看我执行了吗7");

                planeHealth--;
            } else if (this.getRec().intersects(littleBoss2Bullet.getRec())) {
                //碰撞之后出现爆炸动画(在飞机加上生命值后要判断生命值为0才爆炸)
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);
                //我方飞机消失
                this.x = -200;
                this.y = -200;
                GameUtils.removeList.add(this);
                GameUtils.removeList.add(littleBoss2Bullet);

                //游戏失败（飞机血量为0）
                System.out.println("看看我执行了吗7");
                GameCreate.state = 3;
            }
        }
        //8.我方飞机碰撞补给后，补给消失，我方飞机不消失
        for (GiftObj giftObj : GameUtils.giftObjList) {
            if (this.getRec().intersects(giftObj.getRec()) && planeHealth > 0) {
                PlayMusic musicCollect = new PlayMusic();
                musicCollect.play(new File("Musics\\resourseCollect.wav"));
                giftObj.setX(-1000);
                giftObj.setY(-1000);
                GameUtils.removeList.add(giftObj);
                times++;
                planeHealth++;
            }
        }
        //9.我方飞机碰撞boss子弹后，两者都消失
        for (BossBullet bossBullet : GameUtils.bossBulletList) {
            if (this.getRec().intersects(bossBullet.getRec()) && planeHealth > 0) {
                //碰撞之后出现爆炸动画
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);
                bossBullet.setX(-100);
                bossBullet.setY(-100);
                GameUtils.removeList.add(bossBullet);
                System.out.println("看看我执行了吗9");
                planeHealth -= 2;
            } else if (this.getRec().intersects(bossBullet.getRec()) && planeHealth <= 0) {
                //碰撞之后出现爆炸动画
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);
                this.x = -200;
                this.y = -200;
                GameUtils.removeList.add(this);
                GameUtils.removeList.add(bossBullet);
                //游戏失败（飞机血量为0）
                System.out.println("看看我执行了吗9");
                GameCreate.state = 3;
            }
        }
        //10.我方飞机碰撞技能补给后，补给消失，我方飞机不消失
        for (SkillGiftObj skillGiftObj : GameUtils.skillGiftObjList) {
            if (this.getRec().intersects(skillGiftObj.getRec()) && planeHealth > 0) {
                PlayMusic musicCollect = new PlayMusic();
                musicCollect.play(new File("Musics\\resourseCollect.wav"));
                skillGiftObj.setX(-1000);
                skillGiftObj.setY(-1000);
                GameUtils.removeList.add(skillGiftObj);
                skill++;
            }
        }
        //System.out.println("生命值" + planeHealth);
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }


    //绘制生命值的方法
    public void MypaintHP(Graphics g) {
        if (planeHealth >= 1)
            g.drawImage(img, 30, 50, null);
        if (planeHealth >= 2)
            g.drawImage(img, 30 * 2, 50, null);
        if (planeHealth >= 3)
            g.drawImage(img, 30 * 3, 50, null);
        if (planeHealth >= 4)
            g.drawImage(img, 30 * 4, 50, null);
        if (planeHealth >= 5)
            g.drawImage(img, 30 * 5, 50, null);
    }
}